package com.beiye.drivertransport.thematic.learning;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.beiye.drivertransport.R;
import com.beiye.drivertransport.activity.TwoBaseAty;
import com.beiye.drivertransport.adapter.LearnCarAdapter;
import com.beiye.drivertransport.thematic.learning.finishedfragment.ExaminationthematiclearnedFragment;
import com.beiye.drivertransport.thematic.learning.finishedfragment.ExercisesthematiclearnedFragment;
import com.beiye.drivertransport.thematic.learning.finishedfragment.LearnthematiclearnedFragment;
import com.beiye.drivertransport.thematic.learning.finishedfragment.TestthematiclearnedFragment;
import com.squareup.picasso.Picasso;

import org.w3c.dom.Text;

import java.lang.reflect.Field;
import java.util.ArrayList;


import butterknife.OnClick;

//专题培训界面
public class ThematiclearningFinishedActivity extends TwoBaseAty {


    TabLayout TabLayout2;

    ViewPager myviewpager;

    ImageView img_back;

    ImageView img_thematicle;

    TextView tv_thembgm4;

    TextView tv_thembgm5;

    TextView tv_thembgm6;

    TextView tv_thembgm7;

    private ArrayList<Fragment> fragments=new ArrayList<>();
    private ArrayList<String> list_title=new ArrayList<>();
    private LearnCarAdapter adapter;
    private int seqTMark;


    @Override
    public int getLayoutId() {
        return R.layout.activity_thematiclearn;
    }

    public void onClick(View view) {
        if (view.getId() == R.id.img_back) {
            finish();
        }

    }

    @Override
    public void initData() {
        TabLayout2 = (TabLayout) findViewById(R.id.tab_FindFragment_title2);
        myviewpager = (ViewPager) findViewById(R.id.myviewpager);
        img_back = (ImageView) findViewById(R.id.img_back);
        img_thematicle = (ImageView) findViewById(R.id.img_thematicle);
        tv_thembgm4 = (TextView) findViewById(R.id.tv_thembgm4);
        tv_thembgm5 = (TextView) findViewById(R.id.tv_thembgm5);
        tv_thembgm6 = (TextView) findViewById(R.id.tv_thembgm6);
        tv_thembgm7 = (TextView) findViewById(R.id.tv_thembgm7);

        if (Build.VERSION.SDK_INT >= 21) {
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }

        Bundle bundle = getIntent().getExtras();
        String orgId = bundle.getString("orgId");
        String orgName = bundle.getString("orgName");
        int stId = bundle.getInt("stId");
        String rolename = bundle.getString("rolename");
        String adId = bundle.getString("adId");
        int position = bundle.getInt("position");
        int tmenuMark = bundle.getInt("tmenuMark");
        int qmenuMark = bundle.getInt("qmenuMark");
        int peMenuMark = bundle.getInt("peMenuMark");
        int emenuMark = bundle.getInt("emenuMark");
        int sn=bundle.getInt("sn");
        String iconUrl2=bundle.getString("iconUrl2");

        if (iconUrl2 != null) {
            Picasso.with(ThematiclearningFinishedActivity.this).load(Uri.parse(iconUrl2)).placeholder(R.mipmap.no_data2).into(img_thematicle);
        }

        SharedPreferences sp = getSharedPreferences("ThematiclearningActivity",
                Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();
        edit.putInt("tmenuMark", tmenuMark);
        edit.putInt("qmenuMark", qmenuMark);
        edit.putInt("peMenuMark", peMenuMark);
        edit.putInt("emenuMark", emenuMark);
        edit.commit();

        Bundle args = new Bundle();
        args.putInt("sn",sn);
        args.putInt("stId", stId);
        args.putString("orgId", orgId);
        args.putString("orgName", orgName);
        args.putString("rolename", rolename);
        args.putString("adId", adId);
        args.putInt("position", position);
        args.putInt("seqTMark", seqTMark);

        LearnthematiclearnedFragment learnthematiclearningFragment = new LearnthematiclearnedFragment();//学习
        ExaminationthematiclearnedFragment examinationthematiclearningFragment = new ExaminationthematiclearnedFragment();//考试
        TestthematiclearnedFragment testthematiclearningFragment = new TestthematiclearnedFragment();//模拟
        ExercisesthematiclearnedFragment exercisesthematiclearingFragment = new ExercisesthematiclearnedFragment();//习题练习

        learnthematiclearningFragment.setArguments(args);
        examinationthematiclearningFragment.setArguments(args);
        testthematiclearningFragment.setArguments(args);
        exercisesthematiclearingFragment.setArguments(args);

        TextView[] textViews=new TextView[]{tv_thembgm4,tv_thembgm5,tv_thembgm6,tv_thembgm7};
        for(int i=0;i<textViews.length;i++){
            textViews[i].setVisibility(View.GONE);
        }

        list_title = new ArrayList<>();
        if(tmenuMark==1){
            list_title.add("学习");
            fragments.add(learnthematiclearningFragment);
            textViews[0].setVisibility(View.VISIBLE);
        }
        if(emenuMark==1){
            list_title.add("考试");
            fragments.add(examinationthematiclearningFragment);
            textViews[1].setVisibility(View.VISIBLE);
        }
        if(peMenuMark==1){
            list_title.add("模拟");
            fragments.add(testthematiclearningFragment);
            textViews[2].setVisibility(View.VISIBLE);
        }
        if(qmenuMark==1){
            list_title.add("习题练习");
            fragments.add(exercisesthematiclearingFragment);
            textViews[3].setVisibility(View.VISIBLE);
        }

        TabLayout2.setTabMode(TabLayout.MODE_FIXED);
        for(int i=0;i<list_title.size();i++){
            TabLayout2.addTab(TabLayout2.newTab().setText(list_title.get(i)));
        }
        adapter = new LearnCarAdapter(getSupportFragmentManager(), fragments, list_title);
        myviewpager.setAdapter(adapter);
        myviewpager.setOffscreenPageLimit(list_title.size());//解决滑动片段时缓存图片避免回收bug
        TabLayout2.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                View view = tab.getCustomView();
                if (null == view) {
                    tab.setCustomView(R.layout.custom_tab_layout_text);
                }
                TextView textView = tab.getCustomView().findViewById(android.R.id.text1);
                textView.setTextColor(TabLayout2.getTabTextColors());
                textView.setTypeface(Typeface.DEFAULT_BOLD);
                if (textView.getText().toString().trim().equals("学习")) {
                    tv_thembgm4.setBackgroundResource(R.drawable.tab_thempress);
                    tv_thembgm5.setBackgroundResource(R.drawable.tab_themnormal);
                    tv_thembgm6.setBackgroundResource(R.drawable.tab_themnormal);
                    tv_thembgm7.setBackgroundResource(R.drawable.tab_themnormal);
                } else if (textView.getText().toString().trim().equals("考试")) {
                    tv_thembgm4.setBackgroundResource(R.drawable.tab_themnormal);
                    tv_thembgm5.setBackgroundResource(R.drawable.tab_thempress);
                    tv_thembgm6.setBackgroundResource(R.drawable.tab_themnormal);
                    tv_thembgm7.setBackgroundResource(R.drawable.tab_themnormal);

                } else if (textView.getText().toString().trim().equals("模拟")) {
                    tv_thembgm4.setBackgroundResource(R.drawable.tab_themnormal);
                    tv_thembgm5.setBackgroundResource(R.drawable.tab_themnormal);
                    tv_thembgm6.setBackgroundResource(R.drawable.tab_thempress);
                    tv_thembgm7.setBackgroundResource(R.drawable.tab_themnormal);
                } else if (textView.getText().toString().trim().equals("习题练习")) {
                    tv_thembgm4.setBackgroundResource(R.drawable.tab_themnormal);
                    tv_thembgm5.setBackgroundResource(R.drawable.tab_themnormal);
                    tv_thembgm6.setBackgroundResource(R.drawable.tab_themnormal);
                    tv_thembgm7.setBackgroundResource(R.drawable.tab_thempress);
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                View view = tab.getCustomView();
                if (null == view) {
                    tab.setCustomView(R.layout.custom_tab_layout_text);
                }
                TextView textView = tab.getCustomView().findViewById(android.R.id.text1);
                textView.setTypeface(Typeface.DEFAULT);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        TabLayout2.setupWithViewPager(myviewpager);
        setIndicator(this, TabLayout2, 45-10*(list_title.size()-1), 45-10*(list_title.size()-1));
    }

    @Override
    public void requestData() {

    }


    public static void setIndicator(Context context, TabLayout tabs, int leftDip, int rightDip) {
        Class<?> tabLayout = tabs.getClass();
        Field slidingTabIndicator = null;
        try {
            slidingTabIndicator = tabLayout.getDeclaredField("slidingTabIndicator");
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }

        slidingTabIndicator.setAccessible(true);

        LinearLayout ll_tab = null;
        try {
            ll_tab = (LinearLayout) slidingTabIndicator.get(tabs);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

        int left = (int) (getDisplayMetrics(context).density * leftDip);
        int right = (int) (getDisplayMetrics(context).density * rightDip);

        for (int i = 0; i < ll_tab.getChildCount(); i++) {
            View child = ll_tab.getChildAt(i);
            child.setPadding(0, 0, 0, 0);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1);
            params.leftMargin = left;
            params.rightMargin = right;
            child.setLayoutParams(params);
            child.invalidate();
        }
    }


    public static DisplayMetrics getDisplayMetrics(Context context) {
        DisplayMetrics metric = new DisplayMetrics();
        ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(metric);
        return metric;
    }

    public static float getPXfromDP(float value, Context context) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value,
                context.getResources().getDisplayMetrics());
    }
}
